
import re
import sys

import requests


class Spider:

    headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3573.0 Safari/537.36'}

    def get_html(self, url, encoding = None):

        try:

            r = requests.get(url, headers = self.headers)

        except requests.exceptions.ConnectionError:


            return {'Errors':str(sys.exc_info())}

            
        else:
            if encoding == None:

                r.encoding = r.apparent_encoding

            else:

                r.encoding = encoding

            return r.text

    def get_info(self, url, encoding = None, **regex):

        html = self.get_html(url, encoding)

        if isinstance(html, str):

            info = {}

            for key, values in regex.items():


                info[key] = re.findall(values, html)#[:10]

            return info

        elif isinstance(html, dict):

            return html

        

if __name__ == '__main__':

    url = 'https://www.sczprc.com/0/158/'

    regex = 'var booktitle = "(.*?)"'

    x = Spider()

    html = x.get_html(url)#[500:600]
    #info = x.get_info(url,boot_name = regex)

            
































        
